﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.system
{
    public partial class RequestAdd : BaseAdmin
    {
        readonly DALRequestAnswer theRequestAnswer = new DALRequestAnswer();
         tbRequestAnswer oRequestAnswer;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"] == "del")
                {
                    SQLAccess.DoSql("delete from tbRequestAnswer where id=" + Request.QueryString["id"]);
                    base.Response.Write("<script language='javascript'>alert('删除成功！');window.location.href='RequestList.aspx';</script>");
                    base.Response.End();
                    
                }
                this.BildForm();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.txtaContent.InnerText.Trim() == "")
            {
                base.Response.Write("<script language='javascript'>alert('内容不能为空！');</script>");
                return;
            }
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oRequestAnswer = new tbRequestAnswer();
                oRequestAnswer.Request = this.txtaContent.InnerText.Replace("'", "''");
                oRequestAnswer.RequestTime = DateTime.Now;
                oRequestAnswer.RequstUserId = int.Parse(base.AdminUserId);
                theRequestAnswer.Add(oRequestAnswer);
                
            }
            else
            {
                oRequestAnswer = theRequestAnswer.GetModel(int.Parse(Request.QueryString["id"]));
                oRequestAnswer.Answer = this.txtaContent.InnerText.Replace("'", "''");
                oRequestAnswer.AnswerTime = DateTime.Now;
                oRequestAnswer.AnswerUserId = int.Parse(base.AdminUserId);
                theRequestAnswer.Update(oRequestAnswer);
            }

            base.Response.Write("<script language='javascript'>alert('提交成功！');window.location.href='RequestList.aspx';</script>");
        }

        void BildForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                oRequestAnswer = theRequestAnswer.GetModel(int.Parse(Request.QueryString["id"]));
                this.tdRequest.InnerText = oRequestAnswer.Request.Replace("''", "'");
                if (oRequestAnswer.Answer != null)
                {
                    this.txtaContent.InnerText = oRequestAnswer.Answer.Replace("''", "'");
                }
            }
        }
    }
}